package com.kunlun.system.controller;

import com.kunlun.common.model.Page;
import com.kunlun.common.utils.ResponseUtil;
import com.kunlun.system.model.SystemMachineModel;
import com.kunlun.system.model.vo.MachineInstanceVo;
import com.kunlun.system.service.ISystemMachineService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/machine")
public class SystemMachineController {

    private Logger log = LogManager.getLogger();

    @Autowired
    private ISystemMachineService systemMachineService;

    @RequestMapping(value = "/getAllMachine", method = RequestMethod.GET)
    public Object getAllMachine(SystemMachineModel machineModel, int currentPage, int pageSize) {
        try {
            Page<MachineInstanceVo> list = systemMachineService.getAllMachine(machineModel, currentPage, pageSize);
            return ResponseUtil.successResponse(list);
        } catch (Exception e) {
            log.error("MachineController getAllMachine Error: ", e);
            return ResponseUtil.failedResponse("查询虚拟机容器列表数据失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
    public Object downloadTemplate(HttpServletRequest request, HttpServletResponse response, String type) {
        try {
            systemMachineService.downloadTemplate(request, response, type);
        } catch (Exception e) {
            log.error("MachineController getAllMachine Error: ", e);
            return ResponseUtil.failedResponse("查询虚拟机容器列表数据失败！", e.getMessage());
        }
        return null;
    }

    @RequestMapping(value = "/refreshMonitor", method = RequestMethod.GET)
    public Object refreshMonitor() {
        try {
            systemMachineService.collectMonitor();
        } catch (Exception e) {
            log.error("MachineController refreshMonitor Error: ", e);
            return ResponseUtil.failedResponse("刷新服務器監控數據失败！", e.getMessage());
        }
        return null;
    }

    @RequestMapping(value = "/getMachineLineData", method = RequestMethod.GET)
    public Object getMachineLineData(String id) {
        try {
            MachineInstanceVo vo = systemMachineService.getMachineLineData(id);
            return ResponseUtil.successResponse(vo);
        } catch (Exception e) {
            log.error("MachineController getMachineInstance Error: ", e);
            return ResponseUtil.failedResponse("查询虚拟机实例信息失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/getSystemMonitorData", method = RequestMethod.GET)
    public Object getSystemMonitorData(String serviceName, String ipAddress, Integer pageSize) {
        try {
            List<MachineInstanceVo> vos = systemMachineService.getSystemMonitorData(serviceName, ipAddress, pageSize);
            return ResponseUtil.successResponse(vos);
        } catch (Exception e) {
            log.error("MachineController getSystemMonitorData Error: ", e);
            return ResponseUtil.failedResponse("查询虚拟机实例信息失败！", e.getMessage());
        }
    }
}
